<?php
namespace \Absidon\Http;
/**
 * Uniform and Simple HTTP request API.
 *
 * The usage of the term "transport" is given to the implementations to simply calling the classes
 * that implement the various means of PHP. The term is used, not out of ignorance, but necessity to
 * prevent having to write out the correct explanation of the various implementations. Technically,
 * the term fits quite well in what the implementations do. Furthermore, a lot of other libraries
 * make use of the term in their implementations of various protocols (Swift Mailer for one).
 *
 * There will be similarities with the WordPress HTTP API, because I was the original author
 * of the HTTP API. The license of the WordPress HTTP API inherits the WordPress license by default,
 * since no license was specified at the time of the commit and the oversight was found too late.
 * It gets tricky on the process on what can be done to revert it to any other license. The easiest,
 * but time consuming method is to rewrite it. There will be bugs, since many of the bug fixes were
 * copyrighted by another developer and it would be too difficult to ask them to give me permission
 * to use the New-BSD license.
 *
 * All current code either is based on the original patch and fixes that I contributed later or
 * rewritten. Some fixes will not be applied, because I do not feel that the API should do
 * work around PHP bugs and flaws. It is one of the reasons the HTTP requests are so difficult in
 * PHP is that people run into bugs and do not realize it is PHP that is at fault. The Pecl_HTTP
 * extension should fix many of the problems, but unfortuantely, it isn't bundled into PHP and
 * requires PEAR/PECL to install.
 *
 * Warning: this API won't be supported on 100% of PHP hosts. I will make an decision on whether any
 * patches are worth being included (some fixes actually broke features for other people).
 *
 * Note: With PHP5, all of the transports support sending headers (without resorting to hacks) and
 * stream client replaces the fopen transport that is in the WordPress HTTP API.
 *
 * @author Jacob Santos <webmaster@absidongames.com>
 * @package Absidon
 * @subpackage HTTP
 * @since 0.1
 * @license Simplified BSD License <http://www.opensource.org/licenses/bsd-license.html>
 * @license GNU Lesser General Public License 2.1 or later <http://www.opensource.org/licenses/lgpl-2.1.php>
 */

/**
 * The HTTP class tests all of the transports and then falls back during failure.
 *
 * Most of the functionality is found in other classes, this is how it should have been from the
 * beginning of the WordPress HTTP API. Too much was moved into the base HTTP class and bloat was
 * the result. This serves to correct that "mistake", but as an aside, if the code was done this way
 * the core developers at the time might not have allowed the patch to be part of WordPress.
 */
class Http
{

}

/**
 * Returns the initialized Http Object
 *
 * @since 0.1
 * @access private
 *
 * @return Absidon\Http\Http HTTP Transport object.
 */
function _get_object() {
	static $http;

	if ( is_null($http) )
		$http = new Http();

	return $http;
}

/**
 * Retrieve the raw response from the HTTP request.
 *
 * The array structure is a little complex.
 *
 * <code>
 * $res = array( 'headers' => array(), 'response' => array('code' => int, 'message' => string) );
 * </code>
 *
 * All of the headers in $res['headers'] are with the name as the key and the
 * value as the value. So to get the User-Agent, you would do the following.
 *
 * <code>
 * $user_agent = $res['headers']['user-agent'];
 * </code>
 *
 * The body is the raw response content and can be retrieved from $res['body'].
 *
 * This function is called first to make the request and there are other API
 * functions to abstract out the above convoluted setup.
 *
 * @since 0.1
 *
 * @param string $url Site URL to retrieve.
 * @param array $args Optional. Override the defaults.
 * @return array The response.
 */
function remote_request($url, $args = array()) {
	return _get_object()->request($url, $args);
}

/**
 * Retrieve the raw response from the HTTP request using the GET method.
 *
 * @see remote_request() For more information on the response array format.
 *
 * @since 0.1
 *
 * @param string $url Site URL to retrieve.
 * @param array $args Optional. Override the defaults.
 * @return array The response.
 */
function wp_remote_get($url, $args = array()) {
	return _get_object()->get($url, $args);
}

/**
 * Retrieve the raw response from the HTTP request using the POST method.
 *
 * @see remote_request() For more information on the response array format.
 *
 * @since 0.1
 *
 * @param string $url Site URL to retrieve.
 * @param array $args Optional. Override the defaults.
 * @return array The response.
 */
function wp_remote_post($url, $args = array()) {
	return _get_object()->post($url, $args);
}

/**
 * Retrieve the raw response from the HTTP request using the HEAD method.
 *
 * @see remote_request() For more information on the response array format.
 *
 * @since 2.7.0
 *
 * @param string $url Site URL to retrieve.
 * @param array $args Optional. Override the defaults.
 * @return array The response.
 */
function remote_head($url, $args = array()) {
	return _get_object()->head($url, $args);
}

/**
 * Retrieve only the headers from the raw response.
 *
 * @since 0.1
 *
 * @param array $response HTTP response.
 * @return array The headers of the response. Empty array if incorrect parameter given.
 */
function wp_remote_retrieve_headers(&$response) {
	if ( ! isset($response['headers']) || ! is_array($response['headers']))
		return array();

	return $response['headers'];
}

/**
 * Retrieve a single header by name from the raw response.
 *
 * @since 0.1
 *
 * @param array $response
 * @param string $header Header name to retrieve value from.
 * @return string The header value. Empty string on if incorrect parameter given, or if the header doesnt exist.
 */
function wp_remote_retrieve_header(&$response, $header) {
	if ( ! isset($response['headers']) || ! is_array($response['headers']))
		return '';

	if ( array_key_exists($header, $response['headers']) )
		return $response['headers'][$header];

	return '';
}

/**
 * Retrieve only the response code from the raw response.
 *
 * Will return an empty array if incorrect parameter value is given.
 *
 * @since 0.1
 *
 * @param array $response HTTP response.
 * @return string the response code. Empty string on incorrect parameter given.
 */
function wp_remote_retrieve_response_code(&$response) {
	if ( ! isset($response['response']) || ! is_array($response['response']))
		return '';

	return $response['response']['code'];
}

/**
 * Retrieve only the response message from the raw response.
 *
 * Will return an empty array if incorrect parameter value is given.
 *
 * @since 0.1
 *
 * @param array $response HTTP response.
 * @return string The response message. Empty string on incorrect parameter given.
 */
function wp_remote_retrieve_response_message(&$response) {
	if ( ! isset($response['response']) || ! is_array($response['response']))
		return '';

	return $response['response']['message'];
}

/**
 * Retrieve only the body from the raw response.
 *
 * @since 0.1
 *
 * @param array $response HTTP response.
 * @return string The body of the response. Empty string if no body or incorrect parameter given.
 */
function wp_remote_retrieve_body(&$response) {
	if ( ! isset($response['body']) )
		return '';

	return $response['body'];
}